package com.atguigu.cloud.controller;/**
 * @Auter zzh
 * @Date 2024/11/13
 */
import com.atguigu.cloud.entities.User;
import com.atguigu.cloud.sevice.Impl.UserServiceImpl;
import com.atguigu.cloud.tool.load.page.PaginationResult;
import com.atguigu.cloud.tool.resp.ResultData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
/**
 * @projectName: keshe
 * @package: com.atguigu.cloud.controller
 * @className: usercontrollor
 * @author: Eric
 * @description: TODO
 * @date: 2024/11/13 18:35
 * @version: 1.0
 */
@Tag(name = "用户表", description = "用户表增删改")
@RestController
public class Usercontrollor {
    @Resource
    UserServiceImpl UserService;

    //增加用户
    @PostMapping(value = "/user/add")
    @Operation(summary = "添加用户", description = "添加用户,性别传入0,1,一个字符")
    public ResultData<String> adduser(@RequestBody User user){
        System.out.println(user.toString());
        int i = UserService.add(user);
        return ResultData.success("成功插入记录，返回值："+i);
    }

    //删除用户
    @DeleteMapping(value = "/user/del/{id}")
    @Operation(summary = "删除用户", description = "删除用户")
    public ResultData<Integer> deleteuser(@PathVariable("id") Integer id) {
        Integer i=UserService.delete(id);
        return ResultData.success(i);
    }

    //修改用户
    @PutMapping(value = "/user/update")
    @Operation(summary = "修改用户", description = "修改用户")
    public ResultData<String> updateuser(@RequestBody User user){
        int i = UserService.update(user);
        return ResultData.success("成功修改记录，返回值："+i);
    }

    //查询用户
    @GetMapping(value = "/user/get/{id}")
    @Operation(summary = "查询用户", description = "查询用户")
    public ResultData<User> getById(@PathVariable("id") Integer id){
        User user = UserService.getById(id);
        return ResultData.success(user);
    }

    //查询用户列表
    @GetMapping(value = "/user/getall")
    @Operation(summary = "查询用户列表", description = "查询用户列表")
    public ResultData<List<User>> getAll(){
        return ResultData.success(UserService.getAll());
    }

    //查询购物车里的商品
    @GetMapping(value = "/user/getProductList/{id}")
    @Operation(summary = "查询用户购物车", description = "查询用户购物车传入用户id")
    public ResultData<List<Map<String, Object>>> getshopcar(@PathVariable("id") Integer id){
        return ResultData.success(UserService.getProductList(id));
    }
    //删除购物车里的商品
    @DeleteMapping(value = "/user/delProduct/{id}/{products}")
    @Operation(summary = "删除购物车商品", description = "删除购物车商品传入商品id和用户id")
    public ResultData<String> delProduct(@PathVariable("id") Integer id, @PathVariable("products") Integer products){
        return ResultData.success("删除成功记录数"+UserService.delProduct(id, products));
    }
    //下单支付创建订单
    @PostMapping(value = "/user/createOrder")
    @Operation(summary = "创建订单", description = "创建订单传入用户id和商品id")
    public ResultData<String> createOrder(@RequestBody Map<String, Integer> map){
        return ResultData.success("成功创建订单，返回值："+UserService.createOrder(map));
    }

    //分页查询用户
    @PostMapping(value = "/user/paginationQuery/{currentPage}")
    @Operation(summary = "分页查询用户", description = "需要当前页号")
    public ResultData<PaginationResult<User>> paginationQuery(@PathVariable("currentPage") Integer currentPage){
        return ResultData.success(UserService.myPaginationQuery(currentPage));
    }

}
